package kr.ac.mju.mdc.mjuapp.photosns;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import kr.ac.mju.mdc.mjuapp.MainActivity;
import kr.ac.mju.mdc.mjuapp.http.HttpManager;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * @author davidkim
 *
 */
public class PhotoSNSTextThread extends Thread {
	private Handler handler;

	public PhotoSNSTextThread(Handler handler) {
		// TODO Auto-generated constructor stub
		this.handler = handler;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		// debug
		long start = System.currentTimeMillis();
		// create httpManager
		HttpManager httpManager = new HttpManager();
		// init
		httpManager.init();
		// set httpPost
//		httpManager
//				.setHttpPost("http://www.mju.ac.kr/mbs/mjukr/jsp/album/gallery.jsp?boardType=02&boardId=16421523&listType=02&mcategoryId=&row=4&id=mjukr_060204000000");
		httpManager
		.setHttpPost("http://www.mju.ac.kr/mbs/mjukr/jsp/album/gallery.jsp?boardType=02&boardId=16397111&listType=02&mcategoryId=&row=4&id=mjukr_060203000000");
		// httpResponse
		HttpResponse response = null;
		// httpEntity
		HttpEntity entity = null;
		// parsing tag vector
		Vector<String> tagNames = new Vector<String>();
		tagNames.add(HTMLElementName.FORM);
		try {
			// execute login
			response = httpManager.executeHttpPost();
			// get Status
			StatusLine status = response.getStatusLine();
			if (status.getStatusCode() == HttpStatus.SC_OK) {
				// html response result
				entity = response.getEntity();
				// get response elementsMap
				HashMap<String, List<Element>> elementMap = httpManager.getHttpElementsMap(entity, tagNames);
				List<Element> tbodyList = elementMap.get(HTMLElementName.FORM);
				Message msg = new Message();
				msg.what = MainActivity.IMAGE_BOARD_TEXT_COMPLETE;
				msg.obj = parseData(tbodyList);
				handler.sendMessage(msg);
				// debug
				long end = System.currentTimeMillis();
				Log.d("MDC", "ImageBoardTextThread : " + (double) (end - start) / 1000.);
			} else {
				Log.d("MDC", "ImageBoardTextThread : getDataFail");
				handler.sendEmptyMessage(MainActivity.IMAGE_BOARD_TEXT_FAIL);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			Log.d("MDC", "ClientProtocolException : " + e.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("MDC", "IOException : " + e.toString());
		} finally {
			httpManager.shutdown();
		}
	}

	/**
	 * @param elementsList
	 */
	private ArrayList<PhotoSNSInfo> parseData(List<Element> elementsList) {
		Element formElement = elementsList.get(2);
		List<Element> liList = formElement.getAllElements(HTMLElementName.LI);
		ArrayList<PhotoSNSInfo> imgBrdList = new ArrayList<PhotoSNSInfo>();
//		for (Element li : liList) {
		for(int index = 0; index < liList.size(); index++){
			PhotoSNSInfo info = new PhotoSNSInfo();
			Element a = liList.get(index).getFirstElement(HTMLElementName.A);
//			String temp = a.getContent().toString().replaceAll("<{1}.[^<>]*>{1}", "").trim();
//			info.setTitle(temp.substring(0, temp.indexOf("</div>")));
//			info.setName(temp.substring(temp.indexOf("</label>")).replace("새글", "").trim());
			String temp = a.getContent().toString().trim();
			temp = temp.replaceAll("</div>", "*****").replace(">새글<", "").trim();
//			Log.d("MDC", "temp1 : " + temp);
			temp = temp.replaceAll("<{1}.[^<>]*>{1}", "").trim();
//			Log.d("MDC", "temp2 : " + temp);
			info.setTitle(temp.substring(0, temp.indexOf("*****")).trim());
			info.setName(temp.substring(temp.indexOf("*****") + 5).trim());
			imgBrdList.add(info);
			if(index >= 4)
				break;
		}
		return imgBrdList;
	}
}
/* end of file */
